package org.jeecg.modules.monitoring.j_monitoring.job.dataSync;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.monitoring.j_monitoring.entity.*;
import org.jeecg.modules.monitoring.j_monitoring.service.IMGlDriveCabinetService;
import org.jeecg.modules.monitoring.j_monitoring.service.IMGlRegulatorService;
import org.jeecg.modules.monitoring.j_monitoring.service.IMGlStorageResourcesService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Slf4j
public class StorageResourceSyncJob  implements Job {

    @Autowired
    private IMGlStorageResourcesService storageResourcesService;

    @Autowired
    private IMGlRegulatorService regulatorService;

    @Autowired
    private IMGlDriveCabinetService driveCabinetService;
    /***
     * 执行
     * @param jobExecutionContext
     * @throws JobExecutionException
     */
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        log.info(String.format("存储关联数据开始--时间:" + DateUtils.getTimestamp()));
        List<MGlStorageResources> dataList = storageResourcesService.list();
        MGlStorageResources resources;
        MGlRegulator regulator;
        MGlDriveCabinet  driveCabinet;
        JSONArray children,newChildren;
        JSONObject child;
        String cId;
        for (int i = 0; i < dataList.size(); i++) {
            resources = dataList.get(i);
            if(StringUtils.isNotBlank(resources.getCabinetList())){
                //盘柜的处理
                children = (JSONArray) (JSONArray.parseArray(resources.getCabinetList()));
                newChildren = new JSONArray();
                for (int j = 0; j < children.size(); j++) {
                    child = children.getJSONObject(j);
                    cId = null;
                    if(child.get("id")!=null){
                        cId = child.getString("id");
                    }
                    else if(child.get("cabinetId")!=null){
                        cId = child.getString("cabinetId");
                    }
                    if(cId==null){
                        continue;
                    }
                    driveCabinet = driveCabinetService.getById(cId);
                    if(driveCabinet!=null){
                        child.put("id",driveCabinet.getId());
                        child.put("xh",driveCabinet.getModel());
                        child.put("sl",driveCabinet.getHardDisksNo());
                        child.put("zrl",driveCabinet.getSingleHardDiskNo());
                        child.put("jg",driveCabinet.getCabinet());
                        child.put("quw",driveCabinet.getUseUQ());
                        child.put("huw",driveCabinet.getUseUH());
                        child.put("ugd",driveCabinet.getHeight());
                    }

                    newChildren.add(child);
                }
                String value = newChildren.toJSONString();
                resources.setCabinetList(value);
            }

            if(StringUtils.isNotBlank(resources.getRegulatorList())){
                //控制器的处理
                children = (JSONArray) (JSONArray.parseArray(resources.getRegulatorList()));
                newChildren = new JSONArray();
                for (int j = 0; j < children.size(); j++) {
                    child = children.getJSONObject(j);
                    cId = null;
                    if(child.get("id")!=null){
                        cId = child.getString("id");
                    }
                    else if(child.get("regulatorId")!=null){
                        cId = child.getString("regulatorId");
                    }
                    if(cId==null){
                        continue;
                    }
                    regulator = regulatorService.getById(cId);
                    if(regulator!=null){
                        child.put("id",regulator.getId());
                        child.put("xh",regulator.getModel());
                        child.put("sl",regulator.getHardDisksNo());
                        child.put("zrl",regulator.getSingleHardDiskNo());
                        child.put("jg",regulator.getCabinet());
                        child.put("quw",regulator.getUseUQ());
                        child.put("huw",regulator.getUseUH());
                        child.put("ugd",regulator.getHeight());
                    }

                    newChildren.add(child);
                }
                String value = newChildren.toJSONString();
                resources.setRegulatorList(value);
            }
            storageResourcesService.saveOrUpdate(resources);
        }

        log.info(String.format("存储关联数据结束---时间:" + DateUtils.getTimestamp()));
    }
}
